<template>
  <div :class='s.wrap'>
    <div :class='s.inputWrap'>
      <img src="~assets/images/edit.png">
      <input type="" name="" placeholder="写评论" v-model='msg'>
    </div>
    <div :class='s.sendWrap'>
      <div :class='s.bt' @click='onSend' v-if='!isSending'>发送</div>
      <loading v-else></loading>
    </div>
  </div>
</template>

<script>
import SessionDAO from 'common/sessionDAO'
import RemoteDAO from 'common/RemoteDAO'
import checkLogin from 'components/checkLogin'
import Loading from 'components/comment/Loading'
export default {
  components: { Loading },
  props: {
    voteId: String,
    voteTitle: String
  },
  data () {
    return {
      userCode: '',
      msg: '',
      isSending: false
    }
  },
  mounted () {
    this.userCode = SessionDAO.getUserCode()
  },
  methods: {
    onSend () {
      checkLogin().then((userCode) => {
        this.userCode = userCode
        this.sendMsg()
      }).catch(error => {
        this.$store.dispatch('setTip', {text: error.message})
      })
      // this.sendMsg()
    },
    sendMsg () {
      if (this.msg === '' || this.isSending) return
      this.isSending = true
      var msg = this.formatMsg(this.msg)
      let params = {
        userCode: this.userCode,
        actionType: '00',
        businType: 'NRNG',
        communityType: '1',
        objectType: 9, // 9用于股林大会的评论查询
        communityContent: msg,
        topicCode: this.voteId,
        topicName: this.voteTitle
      }
      RemoteDAO.callUC12008306(params).then(resp => {
        console.info('sendMsg', resp)
        this.msg = ''
        this.isSending = false
      }).catch(error => {
        this.$store.dispatch('setTip', {text: error.message})
        this.isSending = false
      })
    },
    formatMsg (msg) {
      msg = escape(msg).replace(/\%u/g, '\\u')
      // msg = this.b64EncodeUnicode(msg)
      return msg
    }
  }
}
</script>
<style lang="less" module='s'>
.wrap{
  background-color: #fff;
  padding: 15px;
  display: flex;
  align-items: center;
}
.inputWrap{
  display: flex;
  align-items: center;
  border: solid 1px #e6e6e6;
  border-radius: 50px;
  padding: 10px;
  flex: 6;
  img{
    width: 6%;
  }
  input{
    flex: 1;
    margin-left: 5px;
    font-size: 26px;
  }
}
.sendWrap{
  flex: 1;
  text-align: center;
  .bt{
    margin-left: 10px;
    padding: 5px;
    font-size: 25px;
  }
}

</style>
